class SessionsController < ApplicationController
  include AuthenticatedSystem
    
  # render new.rhtml
  def new
  end

  def create
    self.current_user = User.authenticate(params[:login], params[:password])
    render :action => 'new' and return unless request.post?
    if logged_in?
      if params[:remember_me] == "1"
        self.current_user.remember_me
        cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
      end
      flash[:notice] = "Bienvenido!"
      if session['invitation'].nil?
        redirect_to user_home_url(current_user.login)
      else
        redirect_to accept_invitation_url(session['invitation'])
      end
    else
      flash[:alert] = "Comprueba tus datos e inténtalo de nuevo"
      render :action => 'new'
    end
  end

  def destroy
    self.current_user.forget_me if logged_in?
    cookies.delete :auth_token
    reset_session
    flash[:notice] = "Hasta pronto!"
    redirect_back_or_default('/')
  end
end
